package com.css.gdzj.gdzjbxgd.modules.statisticAnalysis.service.impl;

import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.BxgdReportInfoNativeRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.BxgdReportInfoRepository;
import com.css.gdzj.gdzjbxgd.modules.statisticAnalysis.service.StaticAnalysisService;
import com.css.gdzj.gdzjbxgd.modules.userinfo.repository.UserInfoNativeRepository;
import com.css.gdzj.gdzjbxgd.modules.userinfo.repository.UserInfoRepository;
import com.css.gdzj.gdzjbxgd.modules.userinfo.repository.entity.UserInfoEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wxx
 * @date 2020-12-11
 */
@Service
public class StaticAnalysisServiceImpl implements StaticAnalysisService {

    @Autowired
    UserInfoNativeRepository userInfoNativeRepository;
    @Autowired
    BxgdReportInfoRepository bxgdReportInfoRepository;
    @Autowired
    BxgdReportInfoNativeRepository bxgdReportInfoNativeRepository;
    @Autowired
    UserInfoRepository userInfoRepository;

    @Autowired
    LocalContainerEntityManagerFactoryBean entityManagerFactory;


    @Override
    public List<Object> getUserReportInfos(String reportYear, String userUnit, Integer pageNo, Integer pageSize) {
        String userId = null;
        List<Object> objects = new ArrayList<>();
        Map<String, Object> mapUser = userInfoNativeRepository.queryUserStatistic(userUnit, pageNo, pageSize);
        List<UserInfoEntity> userInfos = (List) mapUser.get("listUserInfo");
        for (UserInfoEntity userInfo : userInfos) {
            String flagReport = "1";
            Map<String, Object> map = new HashMap<>();
            UserInfoEntity entity = new UserInfoEntity();
            entity.setUserId(userInfo.getUserId());
            entity.setUserName(userInfo.getUserName());//用户名
            entity.setUserUnit(userInfo.getUserUnit());//用户单位
            entity.setUserPost(userInfo.getUserPost());//现任职务
            entity.setUserPhone(userInfo.getUserPhone());//手机号
            entity.setUserLeaderPost(userInfo.getUserLeaderPost());//领导职务
            entity.setUserOfficePhone(userInfo.getUserOfficePhone());//办公电话
            userId = userInfo.getUserId();
            int count = 0;

            count = bxgdReportInfoRepository.getUserReportCount(userId, reportYear);
            if (count > 0) {
                flagReport = "0";//已填报
            }
            map.put("userInfo", entity);
            map.put("flagReport", flagReport);
            objects.add(map);
        }
        return objects;
    }

    @Override
    public Page<UserInfoEntity> getUserReportInfos1(String userIsReport, String reportYear, String userUnit, Pageable pageable) {
        Page<UserInfoEntity> objects = null;
       if (userIsReport.equals("0")) {//填报
             UserInfoEntity userInfoEntity = null;
            StringBuilder sql = new StringBuilder();
            sql.append("    SELECT\n" +
                    "        DISTINCT(CREATE_USER),\n" +
                    "        USER_POST,\n" +
                    "        USER_NAME,\n" +
                    "        USER_UNIT,\n" +
                    "        LOGIN_NAME,\n" +
                    "        LOGIN_PASSWORD,\n" +
                    "        USER_LEVEL,\n" +
                    "        USER_RANK,\n" +
                    "        USER_REPORT_FLAG,\n" +
                    "        USER_PHONE,\n" +
                    "        USER_ID,\n" +
                    "        USER_POLITICAL_OUTLOOK,\n" +
                    "        USER_OFFICE_PHONE,\n" +
                    "        USER_LEADER_POST,\n" +
                    "        DEL_FLAG,\n" +
                    "        USER_IS_REPORT \n" +
                    "    FROM\n" +
                    "        ((SELECT\n" +
                    "            report_id,\n" +
                    "            create_user,\n" +
                    "            report_year \n" +
                    "        FROM\n" +
                    "            GDZJBXGD.bxgd_report_info) AS a  \n" +
                    "    LEFT JOIN\n" +
                    "        GDZJBXGD. user_info b \n" +
                    "            ON a.create_user = b.USER_ID\n" +
                    "        ) WHERE\n" +
                    "    a.CREATE_USER IN (\n" +
                    "        SELECT\n" +
                    "            USER_ID \n" +
                    "        FROM\n" +
                    "            GDZJBXGD. user_info \n" +
                    "    ) and 1=1  ");
            int pageSize = pageable.getPageSize();
            int pageNumber = pageable.getPageNumber();
            long offset = pageable.getOffset();

            if(StringUtils.isNotBlank(userUnit)){
                sql.append("and user_unit = :userUnit");
            }
            if(StringUtils.isNotBlank(reportYear)){
                sql.append("and report_year = :reportYear");
            }
            sql.append("    and user_level = '0' \n" +
                    "    and user_report_flag = '1' ");
            EntityManager entityManager = entityManagerFactory.getNativeEntityManagerFactory().createEntityManager();
            Query nativeQuery = entityManager.createNativeQuery(sql.toString(), UserInfoEntity.class);
            if(StringUtils.isNotBlank(userUnit)){
                nativeQuery.setParameter("userUnit",userUnit);
            }
            if(StringUtils.isNotBlank(reportYear)){
                nativeQuery.setParameter("reportYear",reportYear);
            }
            List<UserInfoEntity> resultList = nativeQuery.getResultList();
            int start = pageNumber * pageSize;
            int end = start + pageSize;

            Page<UserInfoEntity> page = new PageImpl<UserInfoEntity>(resultList.subList(start,resultList.size()>end?end:resultList.size()),pageable,resultList.size());
            return  page;
            //objects = userInfoRepository.getIsReportP(reportYear, userUnit, pageable);
            //return objects;
    } else {//未填报
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT\n" +
                    "        * \n" +
                    "    FROM\n" +
                    "        user_info \n" +
                    "    WHERE\n" +
                    "         user_id NOT IN   (\n" +
                    "            SELECT\n" +
                    "                create_user   \n" +
                    "            FROM\n" +
                    "                bxgd_report_info  where ");

            int pageSize = pageable.getPageSize();
            int pageNumber = pageable.getPageNumber();

            if(StringUtils.isNotBlank(reportYear)){
                sql.append(" report_year = :reportYear)");
            }
            if(StringUtils.isNotBlank(userUnit)){
                sql.append(" and user_unit = :userUnit");
            }
            sql.append("  and user_level = '0' \n" +
                    "        and user_report_flag = '1'");
            EntityManager entityManager = entityManagerFactory.getNativeEntityManagerFactory().createEntityManager();
            Query nativeQuery = entityManager.createNativeQuery(sql.toString(), UserInfoEntity.class);
            if(StringUtils.isNotBlank(userUnit)){
                nativeQuery.setParameter("userUnit",userUnit);
            }
            if(StringUtils.isNotBlank(reportYear)){
                nativeQuery.setParameter("reportYear",reportYear);
            }
            List<UserInfoEntity> resultList = nativeQuery.getResultList();
            int start = pageNumber * pageSize;
            int end = start + pageSize;

            Page<UserInfoEntity> page = new PageImpl<UserInfoEntity>(resultList.subList(start,resultList.size()>end?end:resultList.size()),pageable,resultList.size());
            return  page;
            //objects = userInfoRepository.getNotReportP(reportYear, userUnit, pageable);
            //return objects;
    }
    }
}
